package com.hongtong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongtong.auto.JSONAuthentication;
import com.hongtong.entity.TeCur;
import com.hongtong.entity.TeUser;
import com.hongtong.mapper.TeCurMapper;
import com.hongtong.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongtong
 * @since 2022-04-12
 */
@RestController
@RequestMapping("/te-cur")
public class TeCurController extends JSONAuthentication {
    @Autowired
    TeCurMapper teCurMapper;
    @ApiOperation(value = "添加课程")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody TeCur teCur) throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",teCur.getName());
        if(teCurMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("该课程已存在");
            this.WriteJSON(request, response, error);
        }else{
            if(teCurMapper.insert(teCur)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    }
    @ApiOperation(value = "删除课程")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=teCurMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改课程(课程名不可修改)")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody TeCur teCur) throws  Exception{
            if(teCurMapper.updateById(teCur)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
    }
    @ApiOperation(value = "查找所有课程")
    @PostMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<TeCur> shUserPage=new Page<>(current,size);
        Page<TeCur> shUserPages=teCurMapper.selectPage(shUserPage,null);
        List<TeCur> records = shUserPages.getRecords();
        int total=teCurMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有课程通过课程名")
    @PostMapping("/findAllByName")
    public void findAllByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name")String name)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<TeCur> shUserPage=new Page<>(current,size);
        Page<TeCur> shUserPages=teCurMapper.selectPage(shUserPage,wrapper);
        List<TeCur> records = shUserPages.getRecords();
        int total=teCurMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有课程通过教师名")
    @PostMapping("/findAllByUserName")
    public void findAllByUserName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name")String name)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("username",name);
        Page<TeCur> shUserPage=new Page<>(current,size);
        Page<TeCur> shUserPages=teCurMapper.selectPage(shUserPage,wrapper);
        List<TeCur> records = shUserPages.getRecords();
        int total=teCurMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找课程通过课程id")
    @PostMapping("/findAllById")
    public void findAllById(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
        TeCur teCur = teCurMapper.selectById(id);
        if(teCur!=null){
            Result ok = Result.ok().data("teCur",teCur);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

